<include file="../Public/header_user_c" />
<script type="text/javascript">
String.prototype.len=function()
{
    return this.replace(/[^\x00-\xff]/g,"rr").length;
}

String.prototype.len=function()
{
    return this.replace(/[^\x00-\xff]/g,"rr").length;
}

var merchandise=new Array();
var _PUBLIC_ = "__PUBLIC__";
var level = {$level};
var id={$id};
<volist name="vo" id="vo1">
merchandise.push({$vo1.mcd_id});
</volist>

    jQuery(document).ready(function(){
    	jQuery("#yiic_creatfile").click(function(event){
            var speed = 500;
    		event = event? event: window.event;
    		event = event? event:eventTag;
    		var obj = event.srcElement ? event.srcElement:event.target; 
    		event.stopPropagation();
    		var offset = jQuery(obj).offset();
    		jQuery('#shelvesPop').css({top:offset.top + jQuery(obj).height() + "px",left:offset.left});
    		jQuery('#shelvesPop').slideDown(speed);
    	});
        for(i=0;i<merchandise.length;i++){
        	jQuery("#editdetail"+i).click(function(event){
                var speed = 500;
        		event = event? event: window.event;
        		event = event? event:eventTag;
        		var obj = event.srcElement ? event.srcElement:event.target; 
        		event.stopPropagation();
                var id = merchandise[this.id.substring(10,11)];
                jQuery('#itemno').attr('value',jQuery('#itno'+id).attr('innerHTML'));
                 jQuery('#title').attr('value',jQuery('#title'+id).attr('innerHTML'));
        		var offset = jQuery(obj).offset();
                var left = (offset.left+240)<$(window).width()?offset.left:offset.left-240; 
        		jQuery('#detailPop').css({top:offset.top + jQuery(obj).height() + "px",left:left});
                jQuery('#meid').attr('value',id);
        		jQuery('#detailPop').slideDown(speed);
        	});
        }
    });

    function cancelPop(div){
    	var speed = 500;
    	jQuery('#'+div).slideUp(speed);
    	jQuery('#txtShelves').attr('value');
    }
    function createShelves(){
    	var speed = 500;
        var value = parseInt(jQuery('#txtShelves').attr('value'));
    	if(!(/^([1-9]\d*)$/.test( value ))){
    	    alert("文件数必须是正整数！"); 
    	    jQuery('#txtShelves').focus();
    	    return ;
    	}
        if(!(/^([1-9]\d*)$/.test( level ))){
    	    alert("level参数有误！"); 
    	    jQuery('#txtShelves').focus();
    	    return ;
    	}
        $.ajax({
            type: "get",
            data:{ 'count':value,'level':level,'id':id},
            url: "__URL__/addCommodity",
            success: function(data, textStatus){
                var result =eval("("+data+")");
                if(result.states==0){
                    alert(result.msg);
                }else{
                    jQuery('#txtShelves').attr('value','');
                    cancelPop('shelvesPop');
                    completeCreate(result.data);
                }
            }
        });
    }
    //更新详细
    function updateMerchandise(){
    	var speed = 500;
        var meid = parseInt(jQuery('#meid').attr('value'));
        var itemno = parseInt(jQuery('#itemno').attr('value'));
        var title = parseInt(jQuery('#title').attr('value'));
    	if(!(/^[a-zA-Z1-9\.]{1,6}$/.test( itemno  ))){
    	    alert("编号必须是1-6位英文或数字！"); 
    	    jQuery('#txtShelves').focus();
    	    return ;
    	}
    	if(String(title).len()>10){
    	    alert("描述不能超过10个英文字符或5个汉字！"); 
    	    jQuery('#txtShelves').focus();
    	    return ;
    	}
        cancelPop('detailPop');
        jQuery('#upload').submit();
    }
  
function uploadComplete(data,msg,states){
    jQuery('#itemno').attr('value','');
    jQuery('#title').attr('value','');
    jQuery('#logo').attr('value','');
	if(states==0){
		alert(msg);
		//upload.reset();
	}else{
        var data = eval('('+data+')');
        jQuery('#itno'+jQuery('#meid').attr('value')).attr('innerHTML',data.mcd_itemno);
        jQuery('#title'+jQuery('#meid').attr('value')).attr('innerHTML',data.mcd_title);
        if(data.mcd_logo){
            jQuery('#i'+jQuery('#meid').attr('value')).attr('src',_PUBLIC_+'/Uploads/'+data.mcd_logo);
        }
	}
		
}
    
    //创建页面
    function completeCreate(data){
        location.reload(true);
    }
    
    //删除页面
    function delMerchandise(id){
        if(!confirm("确定要删除?")){
            return;
        }

        $.ajax({
            type: "get",
            data:{ 'id':id},
            url: "__URL__/deleteCommodity",
            success: function(data, textStatus){
                var result =eval("("+data+")");
                if(result.states==0){
                    alert(result.msg);
                }else{
                    $("#m"+id).remove();
                    var yiic_filecount = $("#yiic_filecount").attr('innerHTML');
                    $("#yiic_filecount").attr('innerHTML',yiic_filecount-1);
                }
            }
        });
    }
    
    //隐藏文件
    function hideMerchandise(id){
        var flagvalue = $("#flag"+id).attr("value");
        var flag = flagvalue==1?0:1;
        $.ajax({
            type: "post",
            data:{ 'id':id,'flag':flag},
            url: "__URL__/hideCommodity",
            success: function(data, textStatus){
                var result =eval("("+data+")");
                if(result.states==0){
                    alert(result.msg);
                }else{
                    $("#flag"+id).attr("value",flag);
                    if(flagvalue==0){
                        $("#h"+id).attr("src","__PUBLIC__/Images/show.png");
                    }else{
                        $("#h"+id).attr("src","__PUBLIC__/Images/hidden.png");
                    }
                    //alert(result.msg);
                }
            }
        });
    }
    
    //隐藏文件
    function hideMerchandiseall(){
        var flagvalue = $("#hiddenflagall").attr("value");
        var flag = flagvalue==0?1:0;
        $.ajax({
            type: "post",
            data:{ 'id':id,'level':level,'flag':flag},
            url: "__URL__/hideCommodityall",
            success: function(data, textStatus){
                var result =eval("("+data+")");
                if(result.states==0){
                    alert(result.msg);
                }else{
                    location.reload();
                    //alert(result.msg);
                }
            }
        });
    }
</script>
<div id="m_main">
<input type="hidden" id="hiddenflagall" value="{$hiddenflagall}"/>
<ul>
    <volist name="vo" id="vo">
    <li id="m{$vo.mcd_id}">
        <div id="merchandise">
            <div id="m_img">
                <div class="n_img">
                <a target="_blank" href="__APP__/UserAdmin/merchandise_detail/id/{$vo.mcd_id}"><img id="i{$vo.mcd_id}" src="__PUBLIC__/Uploads/{$vo.mcd_logo|default='default_m.png'}"/></a>
                </div>
            </div>
            <div id="n_num">{$vo.mcd_activenum}</div>
            <a href="javascript:delMerchandise('{$vo.mcd_id}')"><img class="close_m" src="__PUBLIC__/Images/close_m.png"/></a>
            <a href="javascript:hideMerchandise('{$vo.mcd_id}')"><img id="h{$vo.mcd_id}" class="hidden_m" src="__PUBLIC__/Images/<if condition="$vo.mcd_hiddenflag eq 0">hidden.png<else/>show.png</if>"/></a>
            <input type="hidden" id="flag{$vo.mcd_id}" value="{$vo.mcd_hiddenflag}"/>
            <a id="editdetail{$key}"><img class="edit_m" src="__PUBLIC__/Images/edit_m.png"/></a>
            <span id="itno{$vo.mcd_id}" class="m_id">{$vo.mcd_itemno}</span>
        </div>
        <span id="title{$vo.mcd_id}" class="m_title">{$vo.mcd_title}</span>
    </li>
    </volist>
</ul>
</div>
<div id="shelvesPop">
	<div>
        <span class="m_label">文件数量:</span>
        <input id="txtShelves" type="text" size="10" />
        <input type="hidden" id="indexShelves" value=""/>
        <input type="hidden" id="mid" value="{$mcd.mcd_id}"/>
    </div>
	<div class="operation">
        <input type="button" value="确认" onclick="javascript:createShelves()" />
        <input type="button" value="取消" onclick="javascript:cancelPop('shelvesPop')" />
    </div>
</div>

<div id="detailPop">
    <form id="upload" name="upload" method='post' action="__URL__/updateDetail/" enctype="multipart/form-data" target="ajaxUpload">
    <iframe id="ajaxUpload" name="ajaxUpload" src="" frameborder="0"  scrolling="no" style="display:none"></iframe>
    <input type="hidden" name="_uploadResponse" value="uploadComplete"/>  
    <ul>
        <li>编号<input id="itemno" name="itemno" type="text" size="20" /></li>
        <li>说明<input id="title" name="title" type="text" size="20" /></li>
        <li>图片<input id="logo" name="logo" type="file" size="11" /></li>
    </ul>
	<div class="operation">
        <input type="hidden" id="meid" name="meid" value=""/>
        <input type="button" value="确认" onclick="javascript:updateMerchandise()" />
        <input type="button" value="取消" onclick="javascript:cancelPop('detailPop')" />
    </div>
    </form>
</div>
<include file="../Public/foot" />
